﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////


#pragma once
#include "GnufToolbar.h"
#include "GnufQtItem.h"
#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>

namespace gnuf {
    class QtToolbarAid;
    class QtLabel;
    class QtToolbarBelt;
    class QtScrollArea;
    class QtBlock;

    /*             QtToolbar
                          \
                +----------o--------+
                |                   |
        +-------+ - - - - - - - - - +-------+
        |       .                   .       |
        |       .                   .       |
        +-------+ - - - - - - - - - +---o---+
                |                   |    \
                +-------------------+    QtToolbarBelt
    */

    class GNUF_BASE_EXPORT QtToolbar final : public QtItemWith<QWidget, Toolbar>
    {
        Q_OBJECT
    public:
        QtToolbar(Toolbar *, Direction dir);
        virtual ~QtToolbar();
        virtual void refresh(EWhatDirty) override;
        inline QtToolbarBelt * qtToolbarBelt() const { return m_qtToolbarBelt; }
        inline QtScrollArea* qtScrollArea() const { return m_qtScrollArea; }
        inline QtToolbarAid * qtToolbarAid() { return m_qtToolbarAid.get(); }
        inline Direction direction() { return m_direction; }
        bool   deleteQtItemOf(const Block * pBlock);
        bool transferQtItemOf(const Block * pBlock, QtToolbar * qtNewParent);
        bool dumpQtItemWith(Item* pItem, int index);
        int expectedHeightOf(const QtBlock *,
            int * pMaxHeightOfButtonCaption = nullptr,
            int * pMaxHeightOfBlockCaption = nullptr) const;
    protected:
        virtual void disconnectFromItem() override;
        // Inherits from QWidget...
        //virtual bool event(QEvent *) override;
        virtual void mousePressEvent(QMouseEvent *) override;
        virtual void mouseReleaseEvent(QMouseEvent *) override;
        virtual void mouseMoveEvent(QMouseEvent *) override;
        virtual void mouseDoubleClickEvent(QMouseEvent *) override;
        virtual void paintEvent(QPaintEvent *) override;
        virtual void resizeEvent(QResizeEvent *) override;

    private:
        void adjustBlockWidth(int);

    private:
        QtLabel* m_qtBreadcrumb;
        QtScrollArea* m_qtScrollArea;
        QtToolbarBelt * m_qtToolbarBelt;
        gcmp::OwnerPtr< QtToolbarAid > m_qtToolbarAid;
        const Direction m_direction;
        struct { QPoint globalPos; bool enabled; } m_dragAndMove;

        friend class Toolbar;
    };

    class QtToolbarBelt final : public QWidget
    {
    public:
        QtToolbarBelt(QtToolbar *, Direction);
        ~QtToolbarBelt();

    protected:
        // Inherits from QWidget...
        virtual void paintEvent(QPaintEvent *) override;
        virtual void wheelEvent(QWheelEvent *) override;
    private:
        QtToolbar * m_qtToolbar;
        Direction m_scrollDirection;
    };
}
